package com.ruoyi.web.controller.business;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.XlyProject;
import com.ruoyi.system.domain.XlyProjectGroup;
import com.ruoyi.system.domain.query.XlyProjectQuery;
import com.ruoyi.system.domain.vo.XlyProjectAndGroupVo1;
import com.ruoyi.system.domain.vo.XlyProjectAndGroupVo2;
import com.ruoyi.system.domain.vo.XlyProjectAndGroupVo3;
import com.ruoyi.system.service.IXlyProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * MyBatis-Plus-Join
 * <a href="https://mybatisplusjoin.com/pages/quickstart/js.html">...</a>
 */
@ApiSupport(order = 9999)
@Api(tags = "示例代码 - sql - join演示")
@RestController
@RequestMapping("/test/a1/project")
public class Example1ProjectController extends BaseController {

    @Autowired
    private IXlyProjectService xlyProjectService;


    /**
     * 官方文档：https://mybatisplusjoin.com/pages/quickstart/ksks.html
     */
    @ApiOperation("left join 带分页展示1")
    @GetMapping("/leftJoin1")
    public TableDataInfo leftJoin1(XlyProject xlyProject) {
        startPage();

        // 一个项目有多个组
        MPJLambdaWrapper<XlyProject> wrapper = new MPJLambdaWrapper<XlyProject>()
                .selectAll(XlyProject.class) // 查询主表全部字段
                .select(XlyProjectGroup::getGroupName) // 查询关联表其中某个字段
                .leftJoin(XlyProjectGroup.class, XlyProjectGroup::getRelProjectId, XlyProject::getId) // 关联表和关联条件
                .like(StringUtils.isNotEmpty(xlyProject.getProjectName()), XlyProject::getProjectName, xlyProject.getProjectName()); // 添加查询条件

        List<XlyProjectAndGroupVo1> userList = xlyProjectService.selectJoinList(XlyProjectAndGroupVo1.class, wrapper); // 映射进Vo

        return getDataTable(userList);
    }


    /**
     * 官方文档：https://mybatisplusjoin.com/pages/core/lambda/select/selectAs.html
     */
    @ApiOperation("left join 带分页展示2")
    @GetMapping("/leftJoin2")
    public TableDataInfo leftJoin2(XlyProject xlyProject) {
        startPage();

        // 一个项目有多个组
        MPJLambdaWrapper<XlyProject> wrapper = new MPJLambdaWrapper<XlyProject>()
                .selectAll(XlyProject.class) // 查询主表全部字段
                .selectAs(XlyProjectGroup::getGroupName, XlyProjectAndGroupVo2::getAawgjoawihg) // 自定义字段映射
                .leftJoin(XlyProjectGroup.class, XlyProjectGroup::getRelProjectId, XlyProject::getId) // 关联表和关联条件
                .like(StringUtils.isNotEmpty(xlyProject.getProjectName()), XlyProject::getProjectName, xlyProject.getProjectName()); // 添加查询条件

        List<XlyProjectAndGroupVo2> userList = xlyProjectService.selectJoinList(XlyProjectAndGroupVo2.class, wrapper); // 映射进Vo

        return getDataTable(userList);
    }

    /**
     * 官方文档：https://mybatisplusjoin.com/pages/core/lambda/select/selectCollection.html
     * 关于对多分页查询
     * 由于嵌套结果方式会导致结果集被折叠，因此分页查询的结果在折叠后总数会减少，所以无法保证分页结果数量正确。
     *
     * 解决办法：查出来后再分页
     */
    @ApiOperation("left join 带分页展示3")
    @GetMapping("/leftJoin3")
    public TableDataInfo leftJoin3() {

        // 一个项目有多个组
        MPJLambdaWrapper<XlyProject> wrapper = new MPJLambdaWrapper<XlyProject>()
                .selectAll(XlyProject.class) // 查询主表全部字段
                .selectCollection(XlyProjectGroup.class, XlyProjectAndGroupVo3::getXlyProjectGroupList) // 一对多字段映射
                .leftJoin(XlyProjectGroup.class, XlyProjectGroup::getRelProjectId, XlyProject::getId); // 关联表和关联条件

        List<XlyProjectAndGroupVo3> userList = xlyProjectService.selectJoinList(XlyProjectAndGroupVo3.class, wrapper); // 映射进Vo

        return startPage(userList);
    }

    @ApiOperation("代码生产的条件查询")
    @PostMapping("/newQuery")
    public TableDataInfo newQuery(@RequestBody XlyProjectQuery xlyProject) {
        startPage(xlyProject);
        return getDataTable(xlyProjectService.selectXlyProjectList(xlyProject));
    }

}
